package com.xu.Volatile;
/**
 *
 * @author xudenghui
 * @create 2023-03-01 11:27
 **/
public class VolatileDemo  extends  Thread{


    public void setRunning(boolean running) {
        isRunning = running;
    }

    /**
     *
     */
    private volatile boolean  isRunning = true;


    @Override
    public void run() {
        System.out.println("进入RUN方法");
        while(isRunning){
            //如果VolatileDemo线程的isRunning不改为false,线程会永远卡在这里
            System.out.println("===================");
        }
        System.out.println("线程停止");
    }


    public static void main(String[] args) throws InterruptedException {
        VolatileDemo volatileDemo  = new VolatileDemo();
        volatileDemo.start();
        Thread.sleep(1000);
        volatileDemo.setRunning(false);
        System.out.println("isRunning的值已经设置了false");
    }
}
